﻿using System;
using System.Collections;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Maticsoft.Common;
using System.Text;

namespace Lawyer.Web
{
    public partial class activityadd : System.Web.UI.Page
    {
        
        Lawyer.BLL.ActivityTypeInfo bll = new Lawyer.BLL.ActivityTypeInfo();
        Lawyer.BLL.ArchiveInfo archiveBll = new Lawyer.BLL.ArchiveInfo();
        Lawyer.BLL.UserInfo userBll = new Lawyer.BLL.UserInfo();


        public string ArchiveID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                ArchiveID = Request.QueryString["id"].ToString();
            }
            catch { }
            if (ArchiveID != "")
            {
                CheckDiv.Visible = false;
                ArchiveDiv.Visible = false;
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            ActivityTypeDataBind();
            ArchiveDataBind();
        }
        public void ActivityTypeDataBind()
        {
            List<Model.ActivityTypeInfo> activityTypeMap = new List<Lawyer.Model.ActivityTypeInfo>();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            ds = bll.GetList("");
            DataTable dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Model.ActivityTypeInfo typeinfo = new Lawyer.Model.ActivityTypeInfo();
                    typeinfo.TypeID = dt.Rows[i][0].ToString();
                    typeinfo.TypeName = dt.Rows[i][1].ToString();
                    activityTypeMap.Add(typeinfo);
                }
            }
            ActivityTypeList.DataSource = activityTypeMap;
            ActivityTypeList.DataTextField = "TypeName";
            ActivityTypeList.DataValueField = "TypeID";
            ActivityTypeList.DataBind();
        }

        //public void LawyerDataBind()
        //{
        //    List<Model.UserInfo> lawyerMap = new List<Lawyer.Model.UserInfo>();
        //    DataSet ds = new DataSet();
        //    ds = userBll.GetList("");
        //    DataTable dt = ds.Tables[0];
        //    if (dt.Rows.Count > 0)
        //    {
        //        for (int i = 0; i < dt.Rows.Count; i++)
        //        {
        //            Model.UserInfo userinfo = new Lawyer.Model.UserInfo();
        //            userinfo.UserID = dt.Rows[i][0].ToString();
        //            userinfo.UserName = dt.Rows[i][1].ToString();
        //            lawyerMap.Add(userinfo);
        //        }
        //    }
        //    LawyerList.DataSource = lawyerMap;
        //    LawyerList.DataTextField = "UserName";
        //    LawyerList.DataValueField = "UserID";
        //    LawyerList.DataBind();
        //}

        public void ArchiveDataBind()
        {
            List<Model.ArchiveInfo> archiveList = new List<Lawyer.Model.ArchiveInfo>();
            Model.UserInfo userinfo = userBll.GetModel(Session["UserName"].ToString());
            string strWhere = "LawyerID = '" + userinfo.UserID + "'";
            DataSet ds = archiveBll.GetList(strWhere);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Model.ArchiveInfo archive = new Lawyer.Model.ArchiveInfo();
                archive.ArchiveID = dt.Rows[i][0].ToString();
                archive.Title = dt.Rows[i][1].ToString();
                archiveList.Add(archive);
            }

            ArchiveList.DataSource = archiveList;
            ArchiveList.DataTextField = "Title";
            ArchiveList.DataValueField = "ArchiveID";
            ArchiveList.DataBind();
        }

        public string GetLawyerID(string LawyerName)
        {
            Model.UserInfo user = userBll.GetModel(LawyerName);
            return user.UserID;
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strErr = "";
            if (this.txtActivityName.Value.Trim().Length == 0)
            {
                strErr += "Activity name can't be empty!";
            }
            if (this.txtActivityDetails.Text.Trim().Length == 0)
            {
                strErr += "Activity details can't be empty!";
            }
            if (!PageValidate.IsDecimal(txtFee.Value.ToString()))
            {
                strErr += "The activity fee format is not right!";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            string ActivityID = Guid.NewGuid().ToString();
            string TypeID = this.ActivityTypeList.Items[ActivityTypeList.SelectedIndex].Text;
            string Title = this.txtActivityName.Value.ToString();
            string Detail = this.txtActivityDetails.Text.ToString();

            string LawyerID = GetLawyerID(Session["UserName"].ToString());
            DateTime StartDate = DateTime.Parse(this.txtStartTime.Value.ToString());
            DateTime EndDate = DateTime.Parse(this.txtEndTime.Value.ToString());
            decimal Fee = decimal.Parse(this.txtFee.Value.ToString());
            int IsArchive = 0;
            if (ArchiveID == "")
            {
                if (checkArchive.Checked && ArchiveList.Items[ArchiveList.SelectedIndex].Value.ToString() != "")
                {
                    ArchiveID = ArchiveList.SelectedValue.ToString();
                    IsArchive = 1;
                }
            }
            else
            {
                IsArchive = 1;
            }
            
            //Status has three type, 1 is has no approve, 2 is approve but not do,3 is do.
            int Status = 1;
            string CreatedBy = LawyerID;
            DateTime CreatedDate = System.DateTime.Now;

            Lawyer.Model.ActivityInfo model = new Lawyer.Model.ActivityInfo();
            model.ActivityID = ActivityID;
            model.Type = TypeID;
            model.Title = Title;
            model.Detail = Detail;
            model.LawyerID = LawyerID;
            model.StartDate = StartDate;
            model.EndDate = EndDate;
            model.Fee = Fee;
            model.ArchiveID = ArchiveID;
            model.IsArchive = IsArchive;
            model.Status = Status;
            model.CreatedBy = CreatedBy;
            model.CreatedDate = CreatedDate;

            Lawyer.BLL.ActivityInfo bll = new Lawyer.BLL.ActivityInfo();
            bll.Add(model);
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "success！", "activity.aspx");
        }
    }
}
